set more off

******************************************************************************************************************************
**************************************************Table S.7, second panel on field data****************************************
******************************************************************************************************************************


local rloop = 250

local N = 54648

local loopcount = 0

local J = 10


forvalues r = 1/`rloop' {

local loopcount = `loopcount'+1
display "LOOP START!"
display "LOOP START!"
display "LOOP START!"
display "LOOP START!"
display "r is `r'"

set seed 600`r'

do programs/makealpha_zrobust.do `J'  `N' 

gen r = `r'
if(`loopcount' > 1) append using zvarrobust.dta
save zvarrobust.dta, replace
}




use flexlogit,clear
set seed 100
*limited z variation method
*tercile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(3)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha1_`i' = _b[x`i']
 }
restore

drop zdecile stdz



*quartile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(4)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha2_`i' = _b[x`i']
 }

restore

drop zdecile stdz

*decile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(10)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha3_`i' = _b[x`i']
 }

restore

drop zdecile stdz


*ventile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(20)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha4_`i' = _b[x`i']
 }

restore

drop zdecile stdz

*Percentile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(100)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha5_`i' = _b[x`i']
 }

restore

drop zdecile stdz


keep if _n == 1
keep chosen

forvalues j=1/5{
 forvalues i=1/3{
 gen alpha`j'_`i'=`alpha`j'_`i''
 }
}
drop chosen 
gen r=.

save point_robustzvar,replace

use point,clear
merge 1:1 r using point_robustzvar 
drop _merge
save point_robustzvar,replace

use point_robustzvar,clear
forvalues i=1/3 {
	drop impliedbeta`i'
	forvalues j=1/5{
		gen impliedbeta`j'_`i'=theoryrat`i'*alpha`j'_`i'
	}
}
save point_robustzvar,replace 

use bootstrap,clear
merge 1:1 r using zvarrobust
drop _merge
forvalues i=1/3 {
	drop impliedbeta`i'
	forvalues j=1/5{
		gen impliedbeta`j'_`i'=theoryrat`i'*alpha`j'_`i'
	}
}
save bootstrap_robustzvar,replace


 use bootstrap_robustzvar,clear
 append using point_robustzvar
forvalues i=1/3 {
	forvalues j=1/5{
		gen impliedbeta`j'_`i'hatt=impliedbeta`j'_`i' if r==.
		egen impliedbeta`j'_`i'hat=sum(impliedbeta`j'_`i'hatt)
        drop impliedbeta`j'_`i'hatt
	}
}
drop if r==. 
forvalues i=1/3{
	forvalues j=1/5{
		sum impliedbeta`j'_`i'
        gen var`j'_`i' =r(Var)
        replace var`j'_`i'=1/var`j'_`i'
		
	}

}
forvalues j=1/5{
	gen sumvar`j'=var`j'_1+var`j'_2+var`j'_3
	forvalues i=1/3{
		gen weight`j'_`i'=var`j'_`i'/sumvar`j'
	}
	
}



append using point_robustzvar

forvalues j=1/5{
	forvalues i=1/3{
		replace weight`j'_`i'=weight`j'_`i'[_n-1] if r==.
	}
	gen waveragebeta`j'=weight`j'_1*impliedbeta`j'_1+weight`j'_2*impliedbeta`j'_2+weight`j'_3*impliedbeta`j'_3 

	gen wbetahatt`j'=waveragebeta`j' if r==. 
	egen wbetahat`j'=sum(wbetahatt`j')
	drop wbetahatt`j' 

	
}
		drop if r==.
forvalues j=1/5{

	gen low`j'=(waveragebeta`j'<=wbetahat`j')
	egen sumlow`j'=sum(low`j')
	gen z`j'_0=invnormal(sumlow`j'/250)
	gen p`j'_1=normal(z`j'_0+z`j'_0-invnormal(0.975))
	gen p`j'_2=normal(z`j'_0+z`j'_0+invnormal(0.975))
	_pctile waveragebeta`j', nq(1000)
	local p1p=max(1,round(p`j'_1*1000))
	local p2p=min(999,round(p`j'_2*1000))
	gen lower`j'=r(r`p1p')
	gen upper`j'=r(r`p2p')
	
}

keep wbetahat* lower* upper* 
keep if _n==1


*flexible approach results
rename (wbetahat1 wbetahat2 wbetahat3 wbetahat4 wbetahat5 lower1 upper1 lower2 upper2 lower3 upper3 lower4 upper4 lower5 upper5) (Tercile_point Quartile_point Decile_point Ventile_point Percentile_point Tercile_lb Tercile_ub Quartile_lb Quartile_ub Decile_lb Decile_ub Ventile_lb Ventile_ub Percentile_lb Percentile_ub)
label var Tercile_point "Tercile point estimate"
label var Tercile_lb "Tercile lower bound of confidence interval"
label var Tercile_ub "Tercile upper bound of confidence interval"
label var Quartile_point "Quartile point estimate"
label var Quartile_lb "Quartile lower bound of confidence interval"
label var Quartile_ub "Quartile upper bound of confidence interval"
label var Decile_point "Decile point estimate"
label var Decile_lb "Decile lower bound of confidence interval"
label var Decile_ub "Decile upper bound of confidence interval"
label var Ventile_point "Ventile point estimate"
label var Ventile_lb "Ventile lower bound of confidence interval"
label var Ventile_ub "Ventile upper bound of confidence interval"
label var Percentile_point "Percentile point estimate"
label var Percentile_lb "Percentile lower bound of confidence interval"
label var Percentile_ub "Percentile upper bound of confidence interval"

*standard approach results are from running standard logit and getting the location score coefficient
use flexlogit,clear
clogit chosen x* z ,group(ID)

*the result reported in Table S.7 is normalized by multiplying all results above by the std of location score (1.45)







